/*
 * @(#)Startup.java	0.0.1-SNAPSHOT
 *
 * Copyright 2009 Telefónica I+D.
 */
package edu.upf.grupE.main;

import edu.upf.grupE.interftest.MainPage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 

/**
 * <p>This class launch a sample of use of the edu.upf.grupE.Component Interface. This class parses the command line and executes the appropiate method.</p>
 * <p>
 *    Sample: To run (it invokes <code>start</code> method) the Component waiting 5000ms.
 * </p>
 * <PRE>java -jar Ilusion --debug 5000 start or
 * java edu.upf.grupE.main.Bootstrap --debug 5000 start
 * </PRE>
 * @version    0.0.1-SNAPSHOT
 * @author     ${developerName}
 */
public final class Bootstrap {

    /**
     * The logger. A new logger <code>edu.upf.grupE.Component</code> is created to
     * add the logs of this Component
     */
    private static final Log log = LogFactory.getLog(Bootstrap.class);
    /**
     * To debug this component is neccessary wait to launch IDE. By default
     * <code>10000</code> miliseconds are assigned
     */
    static final int DEFAULT_DEBUG_SLEEP_TIME = 10000;

    // The start method
    private static final String START_METHOD = "start";
    // The stop method
    private static final String STOP_METHOD = "stop";
    // The method to be executed. By default is <code>START_METHOD</code>
    private static String method = START_METHOD;

    /**
     * <p>
     * This method parses the command line.
     * </p>
     *
     * @param args an Array of String
     *          
     */
    private static void parseArgs(final String args []) {
        int miliseconds = -1;
        for (int i = 0; i < args.length; i++) {
            // debug option
            if (args[i].equals("--debug")) {
                //if there aren't anymore arguments. The DEFAULT_DEBUG_SLEEP_TIME is assigned.
                if (i + 1 == args.length) {
                    miliseconds = DEFAULT_DEBUG_SLEEP_TIME;
                }
                //else
                // trying to assign a next parameter
                try {
                    if (miliseconds == -1) {
                        miliseconds = Integer.parseInt(args[i + 1]);
                    }
                } catch (NumberFormatException e) {
                    if (log.isWarnEnabled()) {
                        log.warn(new StringBuffer(
                                "The debug sleep time must be an integer. It's assign ").append(DEFAULT_DEBUG_SLEEP_TIME).append(
                                " miliseconds by default."));
                    }
                    //if it isn't a Integer then DEFAULT_DEBUG_SLEEP_TIME is assigned
                    miliseconds = DEFAULT_DEBUG_SLEEP_TIME;
                }
            } else if (args[i].equals(START_METHOD)) {
                method = START_METHOD;
            } else if (args[i].equals(STOP_METHOD)) {
                method = STOP_METHOD;
            }

        }
        if (-1 != miliseconds) {
            //Message to activate DEBUGGER
            System.out.println(
                    new StringBuffer("You can activate the debugger in your IDE. I'm waiting ").append(miliseconds).append(" miliseconds."));

            try {
                //The developer can connect IDE to debug this component
                Thread.sleep(miliseconds);
            } catch (InterruptedException e) {
                if (log.isErrorEnabled()) {
                    log.error("Error in Thread Sleep", e);
                }
                System.exit(1);
            }
        }

    }

    /**
     * <p>
     * The method <code>start</code> initialize the execution of the sample of
     * use.
     * </p>
     *
     * @param args an Array of Strings
     */
    public static void start(final String args []) {
        MainPage.main(null);
    }

    /**
     * <p>
     * The method <code>stop</code> stops the execution of the sample of use.
     * </p>
     * <p>
     * If the component isn't a server this method isn't use
     * </p>
     *
     * @param args an Array of Strings
     *          
     */
    private static void stop(final String args[]) {
        throw new UnsupportedOperationException("Not implemented");
    }

    /**
     * <p>
     * The method <code>main</code> launch the application.
     * </p>
     * @param args an Array of Strings
     */
    public static void main(final String args[]) {
        parseArgs(args);
        if (method.equals(START_METHOD)) {
            start(args);
        } else if (method.equals(STOP_METHOD)) {
            stop(args);
        }
    }
}
